var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync')();
var reload = browserSync.reload;
var sprite = require('css-sprite');
var config = require('../gulp-config');

module.exports = function() {
  gulp.task('dev-sass', function() {
    return gulp.src(config.DEV_SCRIPT_PATH + '/**/*.scss')
    .pipe($.sourcemaps.init())
    .pipe($.sass({
      'outputStyle': 'nested',//compressed
      precision: 10,
      includePaths: ['.'],
      onError: console.error.bind(console, 'Sass error:')
    }))
    .pipe($.autoprefixer({
      browsers: config.CSS_SUPPORT_WEBAPP,
    }))
    .pipe($.if(config.SOURCE_MAP), $.sourcemaps.write())
    .pipe(gulp.dest(config.DEV_CSS_PATH))
    .pipe(reload({stream: true}));
  });

  gulp.task('sprite', function() {
    return gulp.src([config.DEV_SLICE_PATH + '/**/*.png', 
      config.DEV_SLICE_PATH + '/**/*.jpg'])
    .pipe(sprite.create({
      name: 'sprite',
      style: '_sprite.scss',
      processor: 'scss'
    }))
    .pipe($.if('*.png'), gulp.dest(config.DEV_IMAGE_PATH), gulp.dest(config.DEV_STYLE_PATH));
  });
}